package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.DeviceStop;
import com.uwlaser.service.DeviceStopService;
import com.uwlaser.service.dto.DeviceStopDto;
import com.uwlaser.service.dto.DeviceStopQueryParam;
import com.uwlaser.service.mapper.DeviceStopMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = DeviceStopService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DeviceStopServiceImpl extends CommonServiceImpl<DeviceStopMapper, DeviceStop> implements DeviceStopService {

    // private final RedisUtils redisUtils;
    private final DeviceStopMapper deviceStopMapper;

    @Override
    public PageInfo<DeviceStopDto> queryAll(DeviceStopQueryParam query, Pageable pageable) {
        IPage<DeviceStop> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<DeviceStop> page = deviceStopMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, DeviceStopDto.class);
    }

    @Override
    public List<DeviceStopDto> queryAll(DeviceStopQueryParam query){
        return ConvertUtil.convertList(deviceStopMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), DeviceStopDto.class);
    }

    @Override
    public DeviceStop getById(Integer id) {
        return deviceStopMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public DeviceStopDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), DeviceStopDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DeviceStopDto resources) {
        DeviceStop entity = ConvertUtil.convert(resources, DeviceStop.class);
        return deviceStopMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(DeviceStopDto resources){
        DeviceStop entity = ConvertUtil.convert(resources, DeviceStop.class);
        int ret = deviceStopMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return deviceStopMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<DeviceStopDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (DeviceStopDto deviceStop : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("设备编码", deviceStop.getDeviceCode());
              map.put("设备名称", deviceStop.getDeviceName());
              map.put("产线编码", deviceStop.getLineCode());
              map.put("工段编码", deviceStop.getSectionCode());
              map.put("工位编码", deviceStop.getStationCode());
              map.put("停机原因", deviceStop.getDownReasonCode());
              map.put("停机开始时间", deviceStop.getBeginTime());
              map.put("停机结束时间", deviceStop.getEndTime());
              map.put("耗时（分）", deviceStop.getTripTime());
              map.put("创建人", deviceStop.getCreateBy());
              map.put("创建时间", deviceStop.getCreateTime());
              map.put("更新人", deviceStop.getUpdateBy());
              map.put("更新时间", deviceStop.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
